package com.example.administrator.text;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import adapter.VoucherAdapter;
import app.BaseActivity;
import app.MyApplication;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import data.SharedPreferencesUtils;
import entity.Voucher;
import util.GreenTreeNetworkUtil;
import util.HomeUtil;
import util.LogUtil;
import util.RequestDataCallback;
import util.StringUtil;
import util.Url;
import view.diaLogView.CrowdRetunDialog;
import view.diaLogView.UploadDialog;

/**
 * 优惠券
 */
public class VoucherActivity extends BaseActivity {
    private String mId,mToken;
    private int mPosition = 1,mItmeTotal = 1;
    private int lastVisibleItem;
    private VoucherAdapter mVoucherAdapter;
    private VoucherAdapter mVoucherAdapter1;
    private UploadDialog mUploadDialog;
    List<Voucher.CanGetBean> mListCanGet;
    List<Voucher.CannotGetBean> mListCannotGet ;

    @Bind(R.id.activity_voucher_frame)  FrameLayout mFrameLoayout;
    @Bind(R.id.activity_voucher_tv1)    TextView  mTextView1;
    @Bind(R.id.activity_voucher_tv2)    TextView  mTextView2;
    @Bind(R.id.activity_voucher_rv1)    RecyclerView mRecyclerView1;
    @Bind(R.id.activity_voucher_rv2)    RecyclerView mRecyclerView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voucher1);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        HomeUtil.getHemeUtilNew().getStatusBar(this, mFrameLoayout);
        mId = (String) SharedPreferencesUtils.getParam(this, HomeUtil.mUseridDB, "");
        mToken = (String) SharedPreferencesUtils.getParam(VoucherActivity.this, HomeUtil.mTokenDB, "");
        LinearLayoutManager linearLayoutManagaer = new LinearLayoutManager(this);
        linearLayoutManagaer.setOrientation(LinearLayout.VERTICAL);
        LinearLayoutManager linearLayoutManagaer1 = new LinearLayoutManager(this);
        linearLayoutManagaer1.setOrientation(LinearLayout.VERTICAL);
        mRecyclerView1.setLayoutManager(linearLayoutManagaer);
        mRecyclerView1.setHasFixedSize(true);
        mRecyclerView2.setLayoutManager(linearLayoutManagaer1);
        mRecyclerView2.setHasFixedSize(true);
        mRecyclerView2.setVisibility(View.GONE);
        initData(7,1);

        mRecyclerView1.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                lastVisibleItem= layoutManager.findLastVisibleItemPosition();

            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(mListCannotGet !=null){
                    LogUtil.eE("","...onScrollStateChanged....1");  //到了底部
                    if(mListCannotGet.size() > 6){
                        if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mVoucherAdapter.getItemCount()) {
                            LogUtil.eE("","...onScrollStateChanged....2");  //到了底部
                            if(mPosition > mItmeTotal){
                                mVoucherAdapter.changeMoreStatus(mVoucherAdapter.LOADING_THREE);
                            }else {
                                mVoucherAdapter.changeMoreStatus(mVoucherAdapter.LOADING_MORE);
                                initData(7,3);
                            }
                        }
                    }
                }
            }
        });
    }

    /**
     * 已领取
     */
    @OnClick(R.id.activity_voucher_tv1)
    void tv1(){
        mTextView1.setTextColor(getResources().getColor(R.color.white8));
        mTextView1.setBackgroundResource(R.color.head_color);
        mTextView2.setTextColor(getResources().getColor(R.color.newfi_ba_true));
        mTextView2.setBackgroundResource(0);
        mRecyclerView1.setVisibility(View.VISIBLE);
        mRecyclerView2.setVisibility(View.GONE);
        if(mVoucherAdapter == null){
            initData(7,1);
        }
    }

    /**
     * 未领取
     */
    @OnClick(R.id.activity_voucher_tv2)
    void tv2(){
        mTextView2.setTextColor(getResources().getColor(R.color.white8));
        mTextView2.setBackgroundResource(R.color.head_color);
        mTextView1.setTextColor(getResources().getColor(R.color.newfi_ba_true));
        mTextView1.setBackgroundResource(0);
        mRecyclerView1.setVisibility(View.GONE);
        mRecyclerView2.setVisibility(View.VISIBLE);
        if(mVoucherAdapter1 == null){
            initData(7,2);
        }
    }

    /**
     * 返回
     */
    @OnClick(R.id.activity_voucher_ll)
    void getFinish(){
        finish();
    }

    private void initData(int pageSize,final int type) {

        if(type == 1 || type ==2 ){
            if(mUploadDialog == null){
                mUploadDialog = new UploadDialog(this,"加载中");
                mUploadDialog.show();
            }else {
                mUploadDialog.show();
            }
        }
        mMap = new HashMap<>();
        mMap.put("id",mId);
        mMap.put("pageSize",pageSize);
        mMap.put("pageNum",mPosition);
        String stringJson = StringUtil.getStringUtilNew().hashMapToJson(mMap); //参数包
        final long time = StringUtil.getStringUtilNew().getTimeCurrent();
        String stirngSign = StringUtil.getStringUtilNew().getSign(Url.getUrlNew().LIST_COUPON,stringJson,mToken,mId,time);
        Map map1 = StringUtil.getStringUtilNew().getMapNew(this,stringJson,mToken,mId,Url.getUrlNew().LIST_COUPON,stirngSign,time);
        getLogger().info("initData()--->fwq--"+MyApplication.getApp().getFWQString()+"stringJson--"+stringJson);
        GreenTreeNetworkUtil.getInstance().doPost(this,MyApplication.getApp().getFWQString(),1,map1, new RequestDataCallback() {
            @Override public void onSuccess(String response) {
                getLogger().info("initData()--->onSuccess--->"+"String--"+response);
                Gson gson = new Gson();
                Voucher voucher = gson.fromJson(response,Voucher.class);
                if(type == 1 || type ==2 ){
                    if(mUploadDialog !=null){
                        mUploadDialog.dismiss();}}
                if(voucher.getCode() == 1){
                    if(type == 1){
                        mPosition ++;
                        mItmeTotal = voucher.getPageCount();
                        mListCannotGet = voucher.getCannotGet();
                        mVoucherAdapter = new VoucherAdapter(VoucherActivity.this,null,mListCannotGet,0);
                        mRecyclerView1.setAdapter(mVoucherAdapter);
                    }else if(type == 2){
                        mListCanGet = voucher.getCanGet();
                        mVoucherAdapter1 = new VoucherAdapter(VoucherActivity.this,mListCanGet,null,2);
                        mRecyclerView2.setAdapter(mVoucherAdapter1);
                    }else if(type == 3){
                        mPosition ++;
                        List<Voucher.CannotGetBean> list;
                        list = voucher.getCannotGet();
                        for (int i = 0; i < list.size(); i++) {
                            mListCannotGet.add(mListCannotGet.size(),list.get(i));
                        }
                        mVoucherAdapter.notifyItemRangeInserted(mListCannotGet.size(),list.size());
                        mVoucherAdapter.changeMoreStatus(mVoucherAdapter.PULLUP_LOAD_MORE);
                    }

                }else if(voucher.getCode() == -2){
                    CrowdRetunDialog crowdRetunDialog = new CrowdRetunDialog(VoucherActivity.this);
                    crowdRetunDialog.show();
                }else if(voucher.getCode() == -1){
                    Toast.makeText(VoucherActivity.this,getString(R.string.FWQ), Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(VoucherActivity.this,"数据异常，请稍后再试", Toast.LENGTH_SHORT).show();
                    mVoucherAdapter.changeMoreStatus(mVoucherAdapter.PULLUP_LOAD_MORE);
                }

            }

            @Override
            public void onFailure(Throwable error) {
                if(type == 1 || type ==2 ){
                    if(mUploadDialog !=null){
                        mUploadDialog.dismiss();}}
                StringUtil.getStringUtilNew().getExceptionCode(VoucherActivity.this, error.toString());
            }
        });
    }
}
